import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

public class Demo_1103 {
    public static void main(String[] args) throws Exception{
        //初始化一个锁
        ReentrantLock lock = new ReentrantLock();

        try {
            //开始执行业务代码之前先上锁
            lock.lock();
            System.out.println("业务代码执行中...");
            TimeUnit.SECONDS.sleep(3);
            throw new Exception("执行出现异常");
        }finally {
            //无论何时都能释放锁
            lock.unlock();
            System.out.println("锁已释放");
        }
    }
}
